<?
require_once '../include/common.inc.php';
//require("../include/php-excel.class.php");
require("../include/excel.php");
if (!checkAdmin()) {
    header("LOCATION:index.php");
    exit;
}

getMyParam('typeselect');
getMyParam('select2');

if ($typeselect == 1) {
	//ļ
    $sql = "SELECT `name`,`namezh` FROM `category` WHERE `id` = '$select2'";
    $tmp_ary = $db->get_one($sql);	
	$file_name = str_replace(" ","_",preg_replace("/<[^>]+>/","",$tmp_ary['name'])). ".xls";
	$category_name=array();
	$category_name['Category_name']=$tmp_ary['name'];
	$category_name['Category_namezh']=$tmp_ary['namezh'];
	
	$data = array();
	$data [] = array_keys($excel_sql_ary);

	//sub Sub Category 
	$subcategory_cat=array();	
	$sql = "SELECT `product_id`,`category_id` FROM `product_category`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$subcategory_cat[$rs["product_id"]] = $rs['category_id'];
	}	
		
	//echo $select2."\n";
	//sub Sub Category 
	$subcategory_name=array();
	$subcategory_namezh=array();
	$sql = "SELECT `id`,`name`,`namezh` FROM `category` ";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$subcategory_name[$rs["id"]] = $rs['name'];
		$subcategory_namezh[$rs["id"]] = $rs['namezh'];				
	}	

	//Finishes
	$finish_id_name = array();	
	$finish_idzh_name = array();
	$sql = "SELECT `id`, `name`,`namezh` FROM `p_finish_set`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$finish_id_name[$rs["id"]] = $rs['name'];
		$finish_idzh_name[$rs["id"]] = $rs['namezh'];
	}	
	//Setting
	$setting_id_name = array();
	$setting_idzh_name = array();
	$sql = "SELECT `id`, `name`,`namezh` FROM `p_setting`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$setting_id_name[$rs['id']] = $rs["name"];
		$setting_idzh_name[$rs['id']]= $rs["namezh"];
	}	
	//Style
	$style_id_name = array();
	$style_idzh_name = array();
	$sql = "SELECT `id`, `name`,`namezh` FROM `p_style`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$style_id_name[$rs['id']] = $rs["name"];
		$style_idzh_name[$rs['id']] = $rs["namezh"];
		
	}	
	//Collection
	$collection_idzh_name = array();
	$sql = "SELECT `id`, `name`,`namezh` FROM `p_collection`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$collection_id_name[$rs['id']] = $rs["name"];
		$collection_idzh_name[$rs['id']] = $rs["namezh"];
	}

	//Febric Category
	$fabric_cat_ary = array();
	$sql = "SELECT `id`, `name` FROM `p_fabric_category`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$fabric_cat_ary[$rs['id']] = $rs["name"];
	}

	//Leather Category
	$leather_cat_ary = array();
	$sql = "SELECT `id`, `name` FROM `p_leather_category`";
	$query = $db->query($sql);
	while($rs = $db->fetch_array($query)) {
		$leather_cat_ary[$rs['id']] = $rs["name"];
	}

    //cat == category 
    $cat_index_ary = array();

  

   // ȡƷ
    $get_cat_ary = array();
	
	//ƷID
	$have_product_id_ary = array();
	
	$product_cat_ary = array();//Ʒ
	
	
	//ȡĿ¼categorise  IDб: category ҳ id Ϊ$select2   product_category   category 
	
	//当前类的子类
	
	$sub_cat_str = GetCategoryTreeIDS($_categories,$select2,false);
	
	
	// sub category
	$product_cat_ary=array();
	
	$sql = "SELECT pc.`product_id` as product_id, pc.`category_id` as category_id,c.`name`,c.`namezh`,pc.`seq` as seq FROM `product_category` pc INNER JOIN `category` c ON pc.`category_id` = c.id WHERE pc.`category_id` IN (" . $sub_cat_str . ") ORDER BY pc.product_id";

 $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $get_cat_ary[$rs['product_id']][$rs['category_id']] = $rs['seq'];
        $have_product_id_ary[] = $rs['product_id'];
		if( ! array_key_exists($rs['category_id'], $product_cat_ary[$rs['product_id']]))
			$product_cat_ary[$rs['product_id']][$rs['category_id']] = array(
				"category" =>$rs['name'],
				"categoryzh" =>$rs['namezh'],"seq" =>$rs['seq']
			);
    }
	
    $have_product_id_str = implode(",", $have_product_id_ary);
	
	// setting 
	$sql = "SELECT * FROM `product_p_setting` WHERE `product_id` IN (" . $have_product_id_str . ")";
	$query = $db->query($sql);
	$product_setting = array();
	$product_setting_zh = array();
	while($rs = $db->fetch_array($query)) {
		if(isset($setting_id_name[$rs['setting_id']]))
		{
			$product_setting[$rs['product_id']][] = $setting_id_name[$rs['setting_id']];
		}
		
		if(isset($setting_idzh_name[$rs['setting_id']]))
		{
			$product_setting_zh[$rs['product_id']][] = $setting_idzh_name[$rs['setting_id']];
		}
	}


	$sql = "SELECT p.id as product_id, p.*,pf.id as finish_id,psl.id as style_id,pcl.id as collection_id FROM `product` p LEFT OUTER JOIN `p_finish_set` pf ON p.finish_id = pf.id"
		." LEFT OUTER JOIN `p_style` as psl ON p.style_id = psl.id"
		." LEFT OUTER JOIN `p_collection` as pcl ON p.collection_id = pcl.id WHERE p.`id` IN (" . $have_product_id_str . ")    ORDER BY p.`code` DESC";

    $query = $db->query($sql);
	
	$strsub="";
    while($rs = $db->fetch_array($query)) {
		//category start
		$tmp = $tmpzh = array();
		foreach($product_cat_ary[$rs['product_id']] as $pc)
		{
			$tmp[] = $pc['category'];
			$tmpzh[] = $pc['categoryzh'];
		}

		$fabric_cate_value = isset($fabric_cat_ary[$rs['fabric_cat']])?$fabric_cat_ary[$rs['fabric_cat']]:'';
		$leather_cate_value = isset($leather_cat_ary[$rs['leather_cat']])?$leather_cat_ary[$rs['leather_cat']]:'';
		$data[] = array(
			$rs['pcode'],
			$rs['code'],
			$rs['bcode'],
			$rs['name'],
			$rs['hidden_flag']==1?'N':'Y',
			$rs['new_flag']==1?'Y':'N',
			$rs['fabric_flag']==1?'Y':'N',
			$rs['leather_flag']==1?'Y':'N',
			$finish_id_name[$rs['finish_id']],
			! empty($fabric_cate_value) ? $fabric_cate_value : $leather_cate_value,
			htmlentities($rs['comment']),
			$rs['pic'],
			$rs['small_pic'],
			htmlentities($rs['price_2']),
			htmlentities($rs['price_1']),
			htmlentities($rs['price_3']),
			htmlentities($rs['upcharge']),
			htmlentities($rs['length']),
			htmlentities($rs['width']),
			htmlentities($rs['height']),
			htmlentities($rs['cbm']),
			htmlentities($rs['width2']),
			htmlentities($rs['length2']),
			htmlentities($rs['height2']),
			htmlentities($rs['cuft']),
			$rs['finishshown'],
			$rs['fabricshown'],
			$rs['leathershown'],
			$rs['marbleshown'],
			$rs['accentshown'],
			$rs['priceshown'],
			isset($product_setting[$rs['id']])?$product_setting[$rs['id']][0]:'',
			(isset($product_setting[$rs['id']]) && count($product_setting[$rs['id']]) > 1) ? $product_setting[$rs['id']][1]:'',
			$style_id_name[$rs['style_id']],
			$collection_id_name[$rs['collection_id']],
			htmlentities($rs['info']),
			! empty($tmp) ? implode('|', $tmp):'',
			$rs['namezh'],
			$rs['commentzh'],
			! empty($tmpzh) ? implode('|', $tmpzh):'',
			isset($product_setting_zh[$rs['id']])?$product_setting_zh[$rs['id']][0]:'',
			(isset($product_setting_zh[$rs['id']]) && count($product_setting_zh[$rs['id']]) > 1) ? $product_setting_zh[$rs['id']][1]:'',
			$style_idzh_name[$rs['style_id']],
			$rs['leathershownzh'],
			$rs['marbleshownzh'],
			$rs['infozh']
		);
    }	//while end
	
	/*$xls = new Excel_XML('UTF-8', false);
	$xls->addArray($data);
	$xls->generateXML($file_name); */

	Excel::Export($file_name,$data);
} elseif ($typeselect == 2) {
    $sql = "SELECT `name`, `type` FROM `inventory` WHERE `id` = '$select2'";
    $tmp_ary = $db->get_one($sql);
	$file_name =  "Inventory_" .str_replace(" ","_",$tmp_ary['name']). ".xls";
    //$file_name = "Inventory_" . $tmp_ary['name'] . ".xls";
	//header("Content-type:application/vnd.ms-excel");
	//header("Accept-Ranges:bytes");
	//header("Content-Disposition:attachment;filename=".$file_name);
	
    $inventory_type = $tmp_ary['type'];
    $inventory_name = $tmp_ary['name'];
		
	header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
	//header("Content-type:application/vnd.ms-excel");
	header("Accept-Ranges:bytes");
	header("Content-Disposition:attachment;filename=".preg_replace("/<[^>]+>/","",$file_name));
	
	//ͷ   table heaer
	

    if ($inventory_type == 1) {
        echo "Item#\tQty\tSpecial Price\tSpecial Price2\tLocation";
    } else {
        echo "Item#\tQty\tSpecial Price\tLocation";
    }

    $sql = "SELECT `product_id`, `inventory_id`, `qty`, `special_price`, `special_price2` FROM `product_inventory` WHERE `inventory_id`='$select2'";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        $inventory_info_ary[$rs['product_id']] = $rs;
        $product_id_ary[] = $rs['product_id'];
    }

    $product_id_ary = array_unique($product_id_ary);
    $product_id_str = implode(",", $product_id_ary);
    $sql = "SELECT * FROM `product` WHERE `id` IN (" . $product_id_str . ") ORDER BY `code` DESC";
    $query = $db->query($sql);
    while($rs = $db->fetch_array($query)) {
        echo "\n";
        echo $rs['code'] . "\t";
        echo $inventory_info_ary[$rs['id']]['qty'] . "\t";
        echo $inventory_info_ary[$rs['id']]['special_price'] . "\t";
        if ($inventory_type == 1) {
            echo $inventory_info_ary[$rs['id']]['special_price2'] . "\t";
        }
        echo $inventory_name . "\t";
    }

    exit;

}

?> 